package com.create404.suishoupai.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.create404.suishoupai.dto.PageVo;
import com.create404.suishoupai.entity.PhotoRecord;
import com.create404.suishoupai.service.IPhotoRecordService;
import com.create404.suishoupai.utils.Result;
import com.create404.suishoupai.vo.PhotoRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author create404
 * @since 2021-07-17
 */
@Api(tags = "记录列表接口")
@RestController
@RequestMapping("/photo-record")
public class PhotoRecordController {
    @Autowired
    private IPhotoRecordService iPhotoRecordService;

    @Value("${file.uploadurl}")
    private String uploadPath;

    @ApiOperation(value = "获取记录列表")
    @GetMapping("/getPhotoRecordList")
    public Result getPhotoRecordList() {
        List<PhotoRecord> photoRecordList = iPhotoRecordService.list();
        return Result.ok(photoRecordList, "获取记录列表成功");
    }

    @ApiOperation(value = "获取带有员工名字的列表")
    @GetMapping("getsortPhotoRecordList")
    public Result getsortPhotoRecordList() {
        List<PhotoRecordVO> photoRecordVOList = iPhotoRecordService.getPhotoRecordList();
        return Result.ok(photoRecordVOList, "获取记录列表成功");
    }

    @ApiOperation(value = "获取类型为环境整改的记录列表")
    @GetMapping("getPhotoRecordListType1")
    public Result getPhotoRecordListType1() {
        List<PhotoRecordVO> photoRecordVOList = iPhotoRecordService.getPhotoRecordListType1();
        return Result.ok(photoRecordVOList);
    }

    @ApiOperation(value = "获取类型为环境分享的记录列表")
    @GetMapping("getPhotoRecordListType2")
    public Result getPhotoRecordListType2() {
        List<PhotoRecordVO> photoRecordVOList = iPhotoRecordService.getPhotoRecordListType2();
        return Result.ok(photoRecordVOList);
    }

    @ApiOperation(value = "新增记录")
    @PostMapping("/addPhotoRecord")
    @ApiImplicitParam(name = "PhotoRecord", value = "记录实体", required = true, paramType = "PhotoType")
    @ResponseBody
    public Result upload(@RequestParam("files") MultipartFile[] files, PhotoRecord photoRecord) {
        File filePath = new File(uploadPath);
        String fileName = "";
        String filesName = "";
        if (!filePath.isDirectory()) {
            //递归生成文件夹
            filePath.mkdirs();
        }
        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];
            if (file.isEmpty()) {
                return Result.error("上传失败，请选择文件");
            }
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");//设置日期格式
            String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
            fileName = date + "-" + i + "-" + file.getOriginalFilename();
            File dest = new File(filePath, fileName);
            try {
                file.transferTo(dest);
                filesName += "http://39.105.110.122:8082/ssp/images/" + fileName + ",";
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        PhotoRecord photoRecord1 = iPhotoRecordService.getLatestOne();
        int id = photoRecord1.getId();
        id += 1;
        photoRecord.setId(id);
        photoRecord.setImage(filesName);
        photoRecord.setTime(LocalDateTime.now());
        photoRecord.setUpdateTime(LocalDateTime.now());
        photoRecord.setCreateTime(LocalDateTime.now());
        photoRecord.setStatus("3");
        if (photoRecord.getResponsibleId().isEmpty()){
            photoRecord.setResponsibleId("999999");
        }
        boolean save = iPhotoRecordService.save(photoRecord);
        if (save) {
            return Result.ok(id, "新建记录成功");
        } else {
            return Result.error("新建记录失败");
        }

    }


    @ApiOperation(value = "编辑记录")
    @ApiImplicitParam(name = "PhotoRecord", value = "记录详细实体", required = true, paramType = "PhotoRecord")
    @PostMapping("/updatePhotoRecord")
    public Result updatePhotoRecord(@RequestBody PhotoRecord photoRecord) {
        photoRecord.setTime(LocalDateTime.now());
        photoRecord.setUpdateTime(LocalDateTime.now());
        boolean save = iPhotoRecordService.updateById(photoRecord);
        if (save) {
            return Result.ok("编辑成功");
        } else {
            return Result.error("编辑失败");
        }
    }

    @ApiOperation(value = "删除一条记录")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", required = true, value = "记录id")})
    @PostMapping("/deletePhotoRecord/{id}")
    public Result deletePhotoRecord(@PathVariable("id") int id) {
        boolean b = iPhotoRecordService.removeById(id);
        if (b) {
            return Result.ok("删除菜单成功");
        } else {
            return Result.error("删除菜单失败");
        }
    }

    @GetMapping("/approveById")
    public Result approveById(int id) {
        QueryWrapper<PhotoRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        PhotoRecord photoRecord = iPhotoRecordService.getOne(queryWrapper);
        if (photoRecord.getStatus().equalsIgnoreCase("2")) {
            photoRecord.setStatus("1");
            boolean updateById = iPhotoRecordService.updateById(photoRecord);
            if (updateById) {
                return Result.ok(photoRecord, "修改状态1成功");
            } else {
                return Result.error("修改状态1失败");
            }
        } else {
            return Result.error("修改状态1失败");
        }
    }

    @GetMapping("/acceptById")
    public Result acceptById(String id) {
        QueryWrapper<PhotoRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        PhotoRecord photoRecord = iPhotoRecordService.getOne(queryWrapper);
        if (photoRecord.getStatus().equalsIgnoreCase("3")) {
            photoRecord.setStatus("2");
            boolean updateById = iPhotoRecordService.updateById(photoRecord);
            if (updateById) {
                return Result.ok(photoRecord, "修改状态2成功");
            } else {
                return Result.error("修改状态2失败");
            }
        } else {
            return Result.error("修改状态2失败");
        }
    }

    @ApiOperation(value = "根据id获取记录实体")
    @GetMapping("/queryById")
    public Result queryById(String id) {
       PhotoRecordVO photoRecordVO = iPhotoRecordService.queryById(Integer.parseInt(id));
        return Result.ok(photoRecordVO);
    }

    @PostMapping("/getMyRecords")
    public Result getMyRecords(@RequestBody PageVo pageVo) {
        String employeeId = pageVo.getEmployeeId();
        Integer currentPage = Integer.valueOf(pageVo.getPageNum());
        Integer pageSize = Integer.valueOf(pageVo.getPageSize());
        Page<PhotoRecordVO> photoRecordPage = new Page<PhotoRecordVO>(currentPage, pageSize);
        IPage<PhotoRecordVO> page = iPhotoRecordService.getMyPhotoRecords(photoRecordPage, employeeId);
        return Result.ok(page);
    }
}
